package com.henu.main.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TimeUtil {

    public static String transformMilToDateString(Long mil) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Instant instant = Instant.ofEpochMilli(mil);
        ZoneId zoneId = ZoneId.systemDefault();
        String dateTime = formatter.format(LocalDateTime.ofInstant(instant, zoneId));
        return dateTime;
    }

    public static Timestamp getNowTimestamp() {
        return Timestamp.valueOf(LocalDateTime.ofInstant(
                Instant.ofEpochMilli(System.currentTimeMillis() / 1000 * 1000), ZoneId.systemDefault()
        ));
    }

    public static String getNowDateString() {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.ofInstant(
                Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.systemDefault()
        ));
    }

    public static String getToday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        return sdf.format(date);
    }

    public static String getToday(String date) {
        return date;
    }

    public static String getYesterday(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = "";
        try {
            Long yesterdayTimestamo = sdf.parse(date).getTime() - 1 * 24 * 60 * 60 * 1000;
            yesterday = sdf.format(new Date(yesterdayTimestamo));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return yesterday;
    }

    public static Timestamp getTimestamp(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Timestamp timestamp = null;
        try {
            timestamp = new Timestamp(sdf.parse(date).getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return timestamp;
    }

}
